package com.fudong.floatwindows;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Handler;
import android.os.Looper;
import android.text.StaticLayout;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

public abstract class BaseCustomTextView extends View implements Runnable{

    StaticLayout staticLayout;
    protected TextPaint textPaint;

    private float textSize;
    private int textColor;
    protected int scrollSpeed;
    protected String text;
    protected long delayed = 0;
    protected boolean isAutoScroll;
    protected Handler handler = new Handler(Looper.getMainLooper());
    public BaseCustomTextView(Context context) {
        super(context);
    }

    public BaseCustomTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context,attrs);
    }

    public BaseCustomTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public BaseCustomTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        staticLayout = null;
    }

    private void init(Context context, @Nullable AttributeSet attrs){
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.BaseCustomTextView);

        textSize = ta.getDimension(R.styleable.BaseCustomTextView_textSize, 50);
        textColor = ta.getColor(R.styleable.BaseCustomTextView_textColor, Color.BLACK);
        scrollSpeed = ta.getInteger(R.styleable.BaseCustomTextView_scrollSpeed, 5);
        text = ta.getString(R.styleable.BaseCustomTextView_text);
        isAutoScroll = ta.getBoolean(R.styleable.BaseCustomTextView_isAutoScroll, false);
        ta.recycle();

        textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
        textPaint.setColor(textColor);
        textPaint.setStyle(Paint.Style.FILL);
        textPaint.setTextSize(textSize);

    }

    public float getTextSize() {
        return textSize;
    }

    public void setTextSize(float textSize) {
        this.textSize = textSize;
        textPaint.setTextSize(textSize);
        invalidate();
    }

    public int getTextColor() {
        return textColor;
    }

    public void setTextColor(int textColor) {
        this.textColor = textColor;
        textPaint.setColor(textColor);
        invalidate();
    }

    public int getScrollSpeed() {
        return scrollSpeed;
    }

    public void setScrollSpeed(int scrollSpeed) {
        this.scrollSpeed = scrollSpeed;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
        staticLayout = null;
        invalidate();
    }

    public long getDelayed() {
        return delayed;
    }

    public void setDelayed(long delayed) {
        this.delayed = delayed;
    }

    public boolean isAutoScroll() {
        return isAutoScroll;
    }

    public void startAutoScroll(){
        isAutoScroll = true;
        handler.removeCallbacksAndMessages(null);
        handler.postDelayed(this,0);
    }

    public void stopAutoScroll(){
        isAutoScroll = false;
        handler.removeCallbacksAndMessages(null);
    }

}
